import werkzeug

from flask import app, send_from_directory
from flask_restful import reqparse

from ext import *

from entity.project.MachineModel import MachineModel


class MachineController(Resource):
    def get(self):
        list = MachineModel.query.all()
        for e in list:
            print(e, 'e')
        return ok(1)


# 根据ID查询： 路径参数： <item_id>
class MachineGetByIdController(Resource):
    def get(self, item_id):
        machine = MachineModel.query.get(item_id)
        print(machine)
        return ok(1)


class MachineUploadPictureController(Resource):
    def post(self):
        # 上传图片
        parser = reqparse.RequestParser()
        parser.add_argument('myfile', type=werkzeug.datastructures.FileStorage, location='files')
        args = parser.parse_args()
        image = args['myfile']
        if image:
            image.save('F:/mock/imgs/' + image.filename)
        else:
            return error()
        return ok(1)


class MachineDownloadPictureController(Resource):
    def get(self):
        parser = reqparse.RequestParser(trim=True)
        parser.add_argument('filename', required=True, nullable=False, location=['args'])
        args = parser.parse_args()
        filename = args['filename']
        path = 'F:/mock/imgs/'
        response = send_from_directory(path, filename, as_attachment=True)
        return response
